<?php


/**
 * OWL Sidebar Class
 *
 * @package OWL
 * @subpackage Widget
 * @author trung
 *
 * This class is for the purpose of retrieving sidabar widget
 */
class Owl_Sidebar {

    private $registered_sidebars = array();
    private $sidebars_widgets = array();

    function Owl_Sidebar(){
        $this->registered_sidebars = array();
        $this->sidebars_widgets = get_option('sidebars_widgets',array());
    }
    public function get_registered_sidebars(){
        if (is_array($this->registered_sidebars))
            return $this->registered_sidebars;
        else
            return null;
    }
    public function register_sidebar($args = ''){

        $i = count($this->registered_sidebars) + 1;

        $defaults = array(
                            'name' => sprintf('Sidebar %d', $i ),
                            'id' => "sidebar-$i",
                            'before_sidebar' => '<div class="%1$s">',
                            'after_sidebar' => "</div>\n",
                            'before_widget' => '<li id="%1$s" class="widget %2$s">',
                            'after_widget' => "</li>\n",
                            'before_title' => '<h2 class="widget-title">',
                            'after_title' => "</h2>\n",
                            'before_content'=>"<div>\n",
                            'after_content'=>"</div>\n",
                          );

       $sidebar = array_merge($defaults, (array) $args);

       $this->registered_sidebars[$sidebar['id']] = $sidebar;

       return $sidebar['id'];
    }

    public function unregister_sidebar($name){
       	if ( isset( $this->registered_sidebars[$name] ) )
		unset( $this->$registered_sidebars[$name] );
    }
    /**
     *
     * Retrieve full list of sidebars and their widgets.
     *
     *
     * @since 1.0
     * @access private
     *
     * @param bool $update Optional, default is true. Whether to save upgrade of widget array list.
     * @return array Upgraded list of widgets to version 2 array format.
     */
    public function get_sidebar_widget($index=''){       
        $this->sidebars_widgets = get_option('sidebars_widgets',array());        
        if (empty ($index)){
            return $this->sidebars_widgets;
        } else {
            if (empty ($this->registered_sidebars[$index]) || !array_key_exists($index, $this->sidebars_widgets) || !is_array($this->sidebars_widgets[$index]) || empty ($this->sidebars_widgets[$index]))
            return array();
        else
            return $this->sidebars_widgets[$index];
        }

    }

    public function get_sidebar($index,$editable = false){
       global $owl_widget_control;

       if ( is_int($index) ) {
		$index = "sidebar-$index";
        } else {
            foreach ( (array) $this->registered_sidebars as $key => $value ) {
                if ( $value['name'] == $index ) {
                    $index = $key;
                    break;
                }
            }
        }
        
        $sidebar = $this->get_sidebar_widget($index);
        $display = $this->registered_sidebars[$index];
        if ($editable) {
            echo(sprintf($display['before_sidebar'],$display['name']));
            echo "<div id ='$index' class = 'sidebar-container widgets-sortables' style='min-height:80px;'>";
            if ($sidebar){
                foreach($this->sidebars_widgets[$index] as $widget_id){
                        $owl_widget_control->show_widget($widget_id,array_slice($display,4,6),$editable);
                }
            }
            echo '</div>';
            echo $display['after_sidebar'];
            return true;
        } else {
            if ($sidebar){
               echo(sprintf($display['before_sidebar'],$display['name']));
               foreach($this->sidebars_widgets[$index] as $widget_id){
                    $owl_widget_control->show_widget($widget_id,array_slice($display,4,6));
                }
                echo $display['after_sidebar'];
                return true;
            } else {
                return false;
            }

        }
    }

    public function save_sidebars_widgets($sidebars_widgets){
        
        global  $owl_widget_control;
        $registered_widgets = $owl_widget_control->get_registered_widgets();
        $list_widgets = '';
        foreach ( $sidebars_widgets as $sidebar => $widgets ) {
                    if ( !is_array($widgets) )
                            continue;
                    $_widgets = array();
                    foreach ( $widgets as $widget ) {
                            $widget_id = split('-', $widget);
                            $setting = $registered_widgets[$widget_id[0]]->get_settings();

                            if ( !empty($setting) )
                                    $_widgets[] = $widget;
                            else
                                    $list_widgets .= $widget.' ';
                    }
                    $sidebars_widgets[$sidebar] = $_widgets;                   
        }
        update_option('sidebars_widgets', $sidebars_widgets);
        if (empty($list_widgets))
            return '1';
        else
            return trim($list_widgets);
    }
    
    public function get_sidebar_name($index){
        if (empty ($this->registered_sidebars[$index])){
            return false;
        }
        else
            return $this->registered_sidebars[$index]['name'];
    }


}
/**
 * API
 */
 function get_dynamic_sidebar($index){
      global $owl_sidebar_control;
      $lock = get_option('widget_lock');
      if ($lock){
          if (Owl_Functions::current_template() == 'widgets-manager'){
            enqueue_style('default-widget');
            enqueue_script('flip-min');
            enqueue_script('default-widget');
            $owl_sidebar_control->get_sidebar($index,true);
            return true;
          } else {
              if ($owl_sidebar_control->get_sidebar($index,false)){
                  return true;
              } else
                  return false;
          }
      } else {
          $user = Owl_User::get_instance();
          if ($user->is_logged_in()) {
            enqueue_style('default-widget');
            enqueue_script('flip-min');
            enqueue_script('default-widget');
            $owl_sidebar_control->get_sidebar($index,true);
            return true;
          } else {
              if ($owl_sidebar_control->get_sidebar($index,false)){
                  return true;
              } else
                  return false;
          }
      }         
 }
 function register_sidebar($args = ''){
     global $owl_sidebar_control;
     $owl_sidebar_control->register_sidebar($args);
 }

?>